package itbour.onetouchshow.fragment.movie;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;
import com.lsjr.utils.NetUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.challengezone.video.ChooseVideoSizeActivity;
import itbour.onetouchshow.activity.search.SearchActivity;
import itbour.onetouchshow.base.mvp.MVPBaseFragment;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.SpUtils;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.sharedpreference.PreferencesHelper;
import itbour.onetouchshow.utils.video.StatusBarUtils;

/**
 * Created by guoh on 2018/8/1.
 * 功能描述：重构玩大片页面
 * 需要的参数：
 */
public class MovieDesignFragment extends MVPBaseFragment<MovieDesignView, MovieDesignPresenter> implements MovieDesignView {


    @BindView(R.id.v_status_bar)
    View vStatusBar;
    @BindView(R.id.iv_personal_create)
    ImageView ivPersonalCreate;
    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.vp_movies)
    ViewPager vpMovies;

    private HomeDetailslBean basicInfo;
    private List<IdsAndName> typeBeans = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fr_movie_design;
    }

    @Override
    protected void afterCreate(Bundle bundle) {
        initStatusBar();
        basicInfo = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class);

        //是否隐藏自由创建
        if (basicInfo != null && basicInfo.getAppControl() != null) {
            if (basicInfo.getAppControl().getHiddenCustEntrance() == 1) {
                ivPersonalCreate.setVisibility(View.GONE);
            }
        }

        if (basicInfo != null && basicInfo.getTypes() != null && basicInfo.getTypes().size() > 0) {
            for (int i = 0; i < basicInfo.getTypes().size(); i++) {
                AppConst.mSearchIds.add(basicInfo.getTypes().get(i).getSt_id());
                AppConst.mAllType.put(basicInfo.getTypes().get(i).getSt_id(), basicInfo.getTypes().get(i).getName());

                if (basicInfo.getTypes().get(i).getName().equals(AppConst.PLAY_LARGE)) {
                    for (int j = 0; j < basicInfo.getTypes().get(i).getChildren().size(); j++) {
                        if (j == 0) {
                            SpUtils.getInstance().saveString("AppID", new Gson().toJson(basicInfo.getTypes().get(i).getChildren().get(j).getSetIds()));
                        }
                        IdsAndName id = new IdsAndName();
                        id.ids = (ArrayList<Integer>) basicInfo.getTypes().get(i).getChildren().get(j).getSetIds();
                        id.name = basicInfo.getTypes().get(i).getChildren().get(j).getName();
                        titles.add(id.name);
                        typeBeans.add(id);
                    }
                }
            }


            for (int i = 0; i < typeBeans.size(); i++) {
                fragments.add(new MoviePartFragment());
            }

        }
        InnerPagerAdapter pagerAdapter = new InnerPagerAdapter(getFragmentManager(), fragments, typeBeans);
        vpMovies.setAdapter(pagerAdapter);
        vpMovies.setOffscreenPageLimit(2);
        tabLayout.setViewPager(vpMovies, titles.toArray(new String[titles.size()]));
    }


    private void initStatusBar() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) vStatusBar.getLayoutParams();
        params.height = StatusBarUtils.getStatusBarHeight(getActivity());
        vStatusBar.setLayoutParams(params);
        vStatusBar.setBackgroundColor(getResources().getColor(R.color.app_status_bar_color));
    }

    @OnClick({R.id.iv_personal_create, R.id.rl_circular_shape})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_personal_create:
                //自由创建入口
                if (PreferencesHelper.getData(AppConst.KEY_PERSONAL_CREATE_CONFIG) == null) {
                    mPresenter.getFreedomCreateConfigInfo();
                } else {
                    openActivity(ChooseVideoSizeActivity.class);
                }
                break;
            case R.id.rl_circular_shape:
                if (NetUtils.isConnected(getActivity())) {
                    //跳转到搜索界面
                    Intent intent = new Intent(getActivity(), SearchActivity.class);
                    getActivity().startActivity(intent);
                } else {
                    T_.showToastReal("请检查网络");
                }
                break;
        }
    }

    @Override
    public void getConfigFail(String exception) {

    }

    @Override
    public void getConfigSuccess(String response) {
        //获取配置信息成功
        PreferencesHelper.saveData(AppConst.KEY_PERSONAL_CREATE_CONFIG, response);
    }

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

    class InnerPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments = new ArrayList<>();
        private List<IdsAndName> typeBeans;

        public InnerPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments, List<IdsAndName> typeBeans) {
            super(fm);
            this.fragments = fragments;
            this.typeBeans = typeBeans;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return typeBeans.get(position).name;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = fragments.get(position);
            Bundle bundle = new Bundle();
            bundle.putIntegerArrayList(AppConst.KEY_MODULE_IDS, typeBeans.get(position).ids);
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // 覆写destroyItem并且空实现,这样每个Fragment中的视图就不会被销毁
            // super.destroyItem(container, position, object);
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }

    class IdsAndName {
        ArrayList<Integer> ids;
        String name;
    }
}
